package com.example.demo1.aop.aspect;


import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.StringJoiner;


@Component
@Aspect
public class LogAspect {
    @Before("execution(* com.example.demo1.aop.service..*.*(..))")
    public void sysLog(JoinPoint jp) {
        StringJoiner log = new StringJoiner("--", "{", "}");

        Date dNow = new Date();
        SimpleDateFormat ft = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        //添加当前时间
        log.add(ft.format(dNow));

        //当前执行的业务方法名称
        String methodName = jp.getSignature().getName();
        log.add(methodName);

        //方法的参数
        Object[] args = jp.getArgs();
        for (Object arg : args) {
            log.add(arg == null ? "——" : arg.toString());
        }

        System.out.println("日志："+log);
    }
}
